/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: ManagerController
 * Author:   周宏硕
 * Date:     2018/11/2 11:23
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.htwx.controller;

import com.htwx.entity.Manager;
import com.htwx.service.ManagerService;
import com.htwx.util.RSAJS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 周宏硕
 * @create 2018/11/2
 * @since 1.0.0
 */
@RestController
@RequestMapping("ManagerController")
public class ManagerController {

    @Autowired
    private ManagerService managerService;


    @ResponseBody
    @RequestMapping(value = "loginRSA",method = RequestMethod.POST)
    public String loginRSA(Manager manager, HttpServletRequest request) throws Exception {


        String password=RSAJS.sessionDecryptHexStr(request.getSession(),manager.getManagerPass());
        System.out.println("解密后"+password);

        return  "hello";
    }


    //登录验证查询
    @RequestMapping(value = "loginCheck", method = RequestMethod.POST)
    public String loginCheck(@RequestBody Manager data_manager) {

        Manager manager = managerService.selectMangetLoginName(data_manager);
        System.out.println(data_manager);
        if (manager != null) {
            return "success";
        } else {
            return "null";
        }


    }


    //查询所有管理员 如果传入参数的话就查询单个
    @RequestMapping(value = "findManagerAll")
    public List<Manager> findManagerAll(Manager manager) {
        List<Manager> managers = new ArrayList<Manager>();
        try {
            managers = managerService.selectAllManager(manager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return managers;

    }


    //添加一个二级管理员
    @ResponseBody
    @RequestMapping(value = "addManager",method = RequestMethod.POST)
    public Integer addManager(@RequestBody Manager manager) {
        System.out.println(manager);
        Integer result=0;
        try {
            result = managerService.insertManger(manager);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }



    @RequestMapping(value = "deleteManager")
    public  Integer deleteManager(Integer managerId){
        Integer result = 0;
        try {
            result = managerService.deleteManger(managerId);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return  result;
    }

}
